﻿using System.Web;
using System;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
using System.Web.SessionState;

namespace WebLibrary
{
	public class CrossDomainCookie : IHttpModule
	{
		private string m_RootDomain = string.Empty;

		#region IHttpModule Members

		public void Dispose()
		{
			
		}

		public void Init(HttpApplication context)
		{
            m_RootDomain = System.Configuration.ConfigurationManager.AppSettings["RootDomain"];

			Type stateServerSessionProvider = typeof(HttpSessionState).Assembly.GetType("System.Web.SessionState.OutOfProcSessionStateStore");
			FieldInfo uriField = stateServerSessionProvider.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic);

			if (uriField == null)
				throw new ArgumentException("UriField was not found");

			uriField.SetValue(null, m_RootDomain);
			
			context.EndRequest += new System.EventHandler(context_EndRequest);
		}

		void context_EndRequest(object sender, System.EventArgs e)
		{
			HttpApplication app = sender as HttpApplication;

			for (int i = 0; i < app.Context.Response.Cookies.Count; i++)
			{
				app.Context.Response.Cookies[i].Domain = m_RootDomain;
			}
		}

		#endregion
	}
}
